package com.lst.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.ClientOnlineCourseMapper;
import com.lst.dao.OnlineCourseMapper;
import com.lst.entity.response.OnlineCourseListResPara;
import com.lst.model.ClientOnlineCourse;
import com.lst.model.OnlineCourse;
import com.lst.utils.RequestUtils;

public class KungFuListServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    @Autowired
    private OnlineCourseMapper onlineCourseMapper = ctx.getBean(OnlineCourseMapper.class);

    @Autowired
    private ClientOnlineCourseMapper clientOnlineCourseMapper = ctx.getBean(ClientOnlineCourseMapper.class);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	OnlineCourseListResPara resPara = new OnlineCourseListResPara();
	try {
	    BaseRequest reqPara = getReqPara(request);

	    String userid = reqPara.getUserid();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("clientid", userid);
		PageBounds pageBounds = new PageBounds(1,10);
		List<ClientOnlineCourse> coclist = clientOnlineCourseMapper.queryByList(map,pageBounds);
		List<OnlineCourse> featuredList=new ArrayList<>();//我的功夫列表
		if(coclist!=null && coclist.size()>0){
			for(ClientOnlineCourse clientOnlineCourse:coclist){
				OnlineCourse onlineCourse=onlineCourseMapper.selectByPrimaryKey(clientOnlineCourse.getCourseid());
				onlineCourse.setIsAdd(true);
				featuredList.add(onlineCourse);
			}
		}
		
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("enabled", true);
		map2.put("isDeleted", false);
		map2.put("clientid", userid);
		map2.put("reserved8", true);
		map2.put("orderByClause", "oc.CreateDate desc");
		PageBounds pageBounds2 = new PageBounds(1,10-coclist.size());
		List<OnlineCourse> otherList = onlineCourseMapper.queryByList2(map2,pageBounds2);
		

		
		resPara.setFeaturedList(featuredList);
		resPara.setOtherList(otherList);
		resPara.setCode(CommCode.M_Y000000);
		resPara.setMessage(CommCode.M_Y000001);

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}
	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
//	    logger.info("OnlineCourseListServlet jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

//	    logger.info("doPost end run(s): "+ DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public BaseRequest getReqPara(HttpServletRequest request) {

	BaseRequest reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new BaseRequest());

	    reqPara = (BaseRequest) baseRequest.clone();

	    String userid = request.getParameter("userid");

//	    logger.info("reqPara userid: " + userid);

	    reqPara.setUserid(userid);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	OnlineCourseListResPara cgResPara = (OnlineCourseListResPara) resPara;

	if (StringUtils.isEmpty(reqPara.getUserid())) {
	    cgResPara.setCode(CommCode.M_A000000);
	    cgResPara.setMessage(CommCode.M_P000002);
	} else {
	    flag = true;
	}
	return flag;
    }
}
